<?php
	class Doc extends Gdata2AppModel {
		var $defaultParams = array('v'=>3);
		
		function getDocumentList($params=NULL){
			$feed = 'https://docs.google.com/feeds/default/private/full';
			$params = array_merge($this->defaultParams,$params,array('max-results'=>10,'showfolders'=>'true'));
			$result = $this->request('GET',$feed,$params);	
			return $result;
		}

		function uploadFile($params=NULL,$file,$type){
			$filesize = filesize($file);
			$pathinfo = pathinfo($file);
			$custom_header = array(
				'Content-Type: '.$type,
				'Slug: '.$pathinfo['filename'],
				'X-Upload-Content-Type: '.$type,
				'X-Upload-Content-Length: '.$filesize
			);
			$atom = $this->getUploadAtom($file);
			
			$feed = 'https://docs.google.com/feeds/default/private/full';
			$params = array_merge($this->defaultParams,$params);
			
			$result = $this->request('POST',$feed,$params,$atom,$custom_header,1);
			//debug($result);
			return $result;
		}
		
		function getUploadAtom($file){
			$atom = file_get_contents($file);
			return $atom;
		}
		
		function createCollection($params=NULL, $folder=NULL){
			$feed = 'https://docs.google.com/feeds/default/private/full';
			$atom = $this->getCreateCollectionAtom($folder);
			$params = array_merge($this->defaultParams,$params);
			$result = $this->request('POST',$feed,$params,$atom);
			return $result;
		}
		
		function createSubCollection($feed=NULL, $params=NULL, $folder=NULL){
			$atom = $this->getCreateCollectionAtom($folder);
			$params = array_merge($this->defaultParams,$params);
			$result = $this->request('POST',$feed,$params,$atom);
			return $result;
		}
		
		function getCreateCollectionAtom($folder){
			$atom = "<?xml version='1.0' encoding='UTF-8'?>";
			$atom .= "<entry xmlns='http://www.w3.org/2005/Atom'>";
			$atom .= "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#folder'/>";
			$atom .= "<title>".$folder."</title>";
			$atom .= "</entry>";			
			return $atom;
		}	
		
		function getCollectionList($params=NULL,$feed=NULL){
			if(!$feed){
				$feed = 'https://docs.google.com/feeds/default/private/full/-/folder';
			}
			$params = array_merge($this->defaultParams,$params);
			$result = $this->request('GET',$feed,$params);				
			return $result;
		}

		function resumableUpload($feed=NULL,$params=NULL,$file,$type){
			$pathinfo = pathinfo($file);
			$filesize = filesize($file);
			//if($filesize < 262144){
				//$this->uploadFile($params,$file,$type);
			//}else{
				if(!$feed){
					$feed = 'https://docs.google.com/feeds/upload/create-session/default/private/full';
				}
				
				$params = array_merge($this->defaultParams,$params,array('convert'=>'false'));
				
				$custom_header = array(
					'Content-Type: application/atom+xml',
					'X-Upload-Content-Type: '.$type,
					'X-Upload-Content-Length: '.$filesize
				);
				
				$atom = $this->getResumableUploadAtom($pathinfo,$file);
				$result = $this->request('POST',$feed,$params,$atom,$custom_header,1,1);
				if($result){
					$pattern = '/Location: (.*?)\n/';
					if (preg_match($pattern, $result, $location)){
						$upload_uri = $location[1];
						//debug($upload_uri);
						$result = $this->continueUpload($upload_uri,$file,$type,$params);
						return $result;
					}
				}	
				//debug($result);
				//return $result;
			//}
		}
		
		function getResumableUploadAtom($pathinfo,$filename){
			$atom = "<?xml version='1.0' encoding='UTF-8'?>\r\n";
			$atom .= "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:docs='http://schemas.google.com/docs/2007'>\r\n";
			$atom .= "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#document'/>\r\n";
			$atom .= "<title>".$pathinfo['basename']."</title>\r\n";
			$atom .= "</entry>";
			return $atom;
		}
		
		function continueUpload($upload_uri,$filename,$type,$params,$from=null,$to=null,$custom_header=null){
			$feed = $upload_uri;
			$chunk_size = 262144;
			$filesize = filesize($filename);
			if(!$custom_header){
				
				if(!$from && !$to){
					$from = 0;
					$to = $chunk_size-1;
				}else{
					$from = $from+$chunk_size;
					if($to+$chunk_size >= ($filesize-1)){
						$to = $filesize-1;
					}else{
						$to = $to+$chunk_size;
					}
					$chunk_size = $to-$from+1;
				}
				
				$custom_header = array(
					//'Content-Length: 0',
					'Content-Type: '.$type,
					'X-Upload-Content-Type: '.$type,
					'X-Upload-Content-Length: '.$filesize
				);
			}
			$atom = $this->getUploadAtom($filename);
			$result = $this->request('PUT',$feed,$params,$atom,$custom_header,1,1);
			/*
			if($result){
				$pattern = '/1.1 (.*?) /';
				if (preg_match($pattern, $result, $code)){
					$response_code = $code[1];	
					if($response_code == 308){
						$this->continueUpload($upload_uri,$filename,$type,$params,$from,$to);
					}else if($response_code == 503){
						//$this->continueUpload($upload_uri,$filename,$type,$params,($from-$chunk_size),($to-$chunk_size));
					}else{
						
					}
				}
			}
			*/
			return $result;
		}
		
/*
		function showAcl($params){
			$docs = $this->getDocumentList($params);
			foreach($docs['feed']['entry'] as $doc){
				$resource_id = $doc['resourceId'];
				$this->addAcl($params,$resource_id,'writer','user','lubnaweb.help@gmail.com');
			}
		}
*/
		
		function getAcl($params,$resource_id){
			$feed = 'https://docs.google.com/feeds/default/private/full/'.$resource_id.'/acl';
			$params = array_merge($this->defaultParams,$params);
			$result = $this->request('GET',$feed,$params);
			return $result;
		}
		
		function addAcl($params,$resource_id,$role,$type,$scope){
			$feed = 'https://docs.google.com/feeds/default/private/full/'.$resource_id.'/acl';
			$params = array_merge($this->defaultParams,$params);
			$atom = $this->getAddAclAtom($role,$type,$scope);
			$result = $this->request('POST',$feed,$params,$atom);
			return $result;
		}
		
		function getAddAclAtom($role,$type,$scope){
			$atom = "<?xml version='1.0' encoding='UTF-8'?>\r\n";
			$atom .= "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>\r\n";
			$atom .= "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/acl/2007#accessRule'/>\r\n";
			$atom .= "<gAcl:role value='".$role."'/>\r\n";
			$atom .= "<gAcl:scope type='".$type."' value='".$scope."'/>\r\n";
			$atom .= "</entry>";
			return $atom;
		}	
		
		function search($params=NULL,$q=NULL,$max=null){
			$feed = 'https://docs.google.com/feeds/default/private/full/folder:0B5Tm_OdcKvb1R1hfM1YyTU52c3M/contents';
			//$params = array_merge($this->defaultParams,$params,array('max-results'=>$max,'q'=>$q));
			$params = array_merge($this->defaultParams,$params,array('q'=>$q));
			$result = $this->request('GET',$feed,$params);	
			//debug($result);
			return $result;
		}	
		
		function preventDownload($params,$resource_id){
			$feed = 'https://docs.google.com/feeds/default/private/full/'.$resource_id;
			$custom_header = array(
				'Content-Type: application/atom+xml',
				'If-Match: *'
			);
			$params = array_merge($this->defaultParams,$params,array('v'=>3));
			$atom = $this->getPreventDowloadAtom();
			$result = $this->request('PUT',$feed,$params,$atom,$custom_header);
			return $result;
		}
		
		function getPreventDowloadAtom(){
			$atom = "<?xml version='1.0' encoding='UTF-8'?>\r\n";
			$atom .= "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:docs='http://schemas.google.com/docs/2007'>\r\n";
			$atom .= "<category scheme='http://schemas.google.com/g/2005/labels' term='http://schemas.google.com/g/2005/labels#restricted-download' label='restricted-download'/>\r\n";
			$atom .= "</entry>";
			return $atom;
		}
		
		function move($params,$resource_id=null,$to_folder=null){
			//$feed = 'https://docs.google.com/feeds/default/private/full/'.$to_folder;
			//$feed = 'https://docs.google.com/feeds/default/private/full';
			$feed = 'https://docs.google.com/feeds/folders/private/full/folder%3A'.$to_folder.'/contents';
			$params = array_merge($this->defaultParams,$params,array('v'=>3));
			$atom = $this->getMoveAtom($resource_id);
			$custom_header = array(
				'Content-Type: application/atom+xml',
				'Content-Length: 180'
			);
			//$result = $this->request('PUT',$feed,$params,$atom,$custom_header);
			$result = $this->request('POST',$feed,$params,$atom,$custom_header);
			return $result;
		}
		
		function getMoveAtom($resource_id){
			$atom = "<?xml version='1.0' encoding='UTF-8'?>\r\n";
			$atom .= "<entry xmlns='http://www.w3.org/2005/Atom'>\r\n";
			$atom .= "<id>https://docs.google.com/feeds/documents/private/full/document%3A0B_xLEbqncR7NM3hZZkJ6M1pXM0U</id>\r\n";
			//$atom .= "<id>https://docs.google.com/feeds/id/".$resource_id."</id>\r\n";
			$atom .= "</entry>";
			return $atom;
		}
	}
